/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Com.Checkin_Checkout_Kamar_Case;

import Com.Entity.Kamar;
import Com.Entity.Koneksi;
import com.toedter.calendar.JDateChooser;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import Com.Entity.TransaksiKamar;
import java.awt.event.KeyEvent;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.text.DateFormatter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author Ardha
 */
public class TransaksiKamarController {

    public static void getKamar(javax.swing.JComboBox tipeKamarCombo, javax.swing.JComboBox noKamarCombo) throws Exception {
        try {


            if (tipeKamarCombo.getSelectedIndex() != 0) {
                noKamarCombo.removeAllItems();
                Kamar kamar = new Kamar();
                kamar.setJenis_Kamar(tipeKamarCombo.getSelectedItem().toString().replaceAll(" ROOM", ""));
                List<Kamar> showKamar = Kamar.getKoneksiKontrol().getKamar(kamar);
                for (Kamar kmr : showKamar) {
                    int noKamar = kmr.getNo_Kamar();
                    noKamarCombo.addItem("Room: " + noKamar);
                }
            } else {
                noKamarCombo.removeAllItems();
                noKamarCombo.addItem("-Nomor Kamar - Status Booking-");
            }
        } catch (FileNotFoundException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
    }

    public static void CheckinKamar(TransaksiKamar transkamar) throws SQLException, IOException {

        TransaksiKamar.getKoneksiKontrol().CheckIn(transkamar);
    }

    public static String noTrans(TransaksiKamar transKamar) throws SQLException {
        return transKamar.callNoTransaksi();
    }

    public static int CheckinKamarAction(JTextField noCheckinTextField, JTextField namaText, JTextField alamatText,
            JComboBox noKamarComboBox, JTextField noKTPtext, JComboBox jenisKTPComboBox,
            JDateChooser tanggalLahirChooser, JTextField nomorHPTextField, JTextField jumpenyewaText, JLabel tanggalLabel,
            JComboBox tipekamarComboBox, JTextField tempatlahirText, JRadioButton jenisKelaminwanita, JRadioButton priaRadioButton1) {
        int stat = 0;

//        if (namaText.getText().equals("") || namaText.getText() == null || namaText.getText().isEmpty()) {
//            JOptionPane.showMessageDialog(null, "Nama tamu belum diisi");
//        } else if (alamatText.getText().equals("") || alamatText.getText() == null || alamatText.getText().isEmpty()) {
//            JOptionPane.showMessageDialog(null, "alamat tamu belum diisi");
//        } else if (noKTPtext.getText().equals("") || noKTPtext.getText() == null || noKTPtext.getText().isEmpty()) {
//            JOptionPane.showMessageDialog(null, "no ID belum diisi");
//        } else if (jenisKTPComboBox.getSelectedIndex() == 0) {
//            JOptionPane.showMessageDialog(null, "jenis ID belum dipilih");
//        } else if (tempatlahirText.getText().equals("") || tempatlahirText.getText() == null || tempatlahirText.getText().isEmpty()) {
//            JOptionPane.showMessageDialog(null, "tempat lahir  tamu masih kosong");
//        } else if (tanggalLahirChooser.getDate() == null) {
//            JOptionPane.showMessageDialog(null, "tanggal lahir belum diisi");
//        } else if (!jenisKelaminwanita.isSelected() && !priaRadioButton1.isSelected()) {
//            JOptionPane.showMessageDialog(null, "jenis kelamin belum dipilih");
//        } else if (tipekamarComboBox.getSelectedIndex() == 0) {
//            JOptionPane.showMessageDialog(null, "tipekamar belum dipilih");
//        } else if (nomorHPTextField.getText().equals("") || nomorHPTextField.getText() == null || nomorHPTextField.getText().isEmpty()) {
//            JOptionPane.showMessageDialog(null, "nomor telepon belum diisi");
//        } else if (jumpenyewaText.getText().equals("") || jumpenyewaText.getText() == null || jumpenyewaText.getText().isEmpty()) {
//            JOptionPane.showMessageDialog(null, "jumlah penyewa belum diisi");
//        } else {
            try {
                SimpleDateFormat sDF = new SimpleDateFormat("dd-MM-yyyy");
                String noCheckin = noCheckinTextField.getText();
                String namatamu = namaText.getText();
                String alamat = alamatText.getText();
                String noKamar = noKamarComboBox.getSelectedItem().toString();
                String noktp = noKTPtext.getText();
                String jenisId = jenisKTPComboBox.getSelectedItem().toString();
                String tanggal = sDF.format(tanggalLahirChooser.getDate());
                String nohp = nomorHPTextField.getText();
                String tempatLahir = tempatlahirText.getText();
                int jumpenyewa = Integer.parseInt(jumpenyewaText.getText());
                DateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
                Date date = (Date) formatter.parse(tanggalLabel.getText());
                String tglmasuk = sDF.format(date);
                System.out.println(tglmasuk);
                String tipe = tipekamarComboBox.getSelectedItem().toString();
                TransaksiKamar transKamar = new TransaksiKamar();

                transKamar.setNo_checkin(noCheckin);
                transKamar.setNama_tamu(namatamu);
                transKamar.setAlamatTamu(alamat);
                transKamar.setNomor_kamar(Integer.parseInt(noKamar.substring(6, 9)));
                transKamar.setNo_id(noktp);
                transKamar.setJenis_Id(jenisId);
                transKamar.setTanggalLahir(tanggal);
                transKamar.setNo_telepon(nohp);
                transKamar.setJum_penyewa(jumpenyewa);
                transKamar.setTanggalMasuk(tglmasuk);
                transKamar.setTipe_kamar(tipe);
                transKamar.setTempat_lahir(tempatLahir);

                TransaksiKamarController.CheckinKamar(transKamar);
                String nomor = noKamar.substring(6, 9);
                JOptionPane.showMessageDialog(null, "Check in berhasil ");
                stat = 1;
//            } catch (FileNotFoundException ex) {
//                JOptionPane.showMessageDialog(null, "Checkin gagal");
//                stat = 0;
//            } catch (IOException ex) {
//                JOptionPane.showMessageDialog(null, "Checkin gagal");
//                stat = 0;
//            } catch (SQLException ex) {
//                JOptionPane.showMessageDialog(null, "Checkin gagal");
//                stat = 0;
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
                stat = 0;
            }
//        }


        return stat;
    }

    public static void batalTrans(String notrans) throws FileNotFoundException, IOException, SQLException {
        TransaksiKamar.getKoneksiKontrol().batalTrans(notrans);
    }

    public static int checkout(int nomor, String waktu) throws FileNotFoundException, IOException, SQLException {
        return TransaksiKamar.getKoneksiKontrol().checkOut(nomor, waktu);
    }

    public static void cariKamar(JDialog dialog, java.awt.event.KeyEvent evt, JTextField nokamarsewa, JTextField namaTamuText,
            JTextField alamatText, JTextField tanggalCheckInText, JTextField tipeKamarText,
            JTextField jumpenyewaText, JLabel noCheckIn) {
        if (nokamarsewa.getText().length() == 3) {
            if (Character.isDigit(evt.getKeyChar())) {
                TransaksiKamar carikamar = new TransaksiKamar();
                int nomor = Integer.parseInt(nokamarsewa.getText());
                carikamar.setNomor_kamar(nomor);
                try {
                    TransaksiKamar carikamar2 = TransaksiKamar.getKoneksiKontrol().cariKamar(carikamar);
                    String namatamu = carikamar2.getNama_tamu();
                    String alamat = carikamar2.getAlamatTamu();
                    String tanggal = carikamar2.getTanggalMasuk();
                    String Tipekamar = carikamar2.getTipe_kamar();
                    int jumpenyewa = carikamar2.getJum_penyewa();
                    String noTrans = carikamar2.getNo_checkin();

                    namaTamuText.setText(namatamu);
                    alamatText.setText(alamat);
                    tanggalCheckInText.setText(tanggal);
                    tipeKamarText.setText(Tipekamar);
                    jumpenyewaText.setText("" + jumpenyewa);
                    noCheckIn.setText(noTrans);
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(dialog, "Kamar tidak sedang disewa");
                }
            } else if (!KeyEvent.getKeyText(evt.getKeyCode()).toUpperCase().matches("ENTER")
                    && !KeyEvent.getKeyText(evt.getKeyCode()).toUpperCase().matches("BACKSPACE")) {
                JOptionPane.showMessageDialog(dialog, "Format masukan salah\nnomor kamar adalah angka");
            }
        } else if (nokamarsewa.getText().length() >= 3) {
            JOptionPane.showMessageDialog(dialog, "Format masukan salah\nnomor kamar hanya 3 digit angka");
            String end = nokamarsewa.getText().substring(0, 2);
            nokamarsewa.setText(end);
        } else if (!Character.isDigit(evt.getKeyChar()) && !KeyEvent.getKeyText(evt.getKeyCode()).toUpperCase().matches("ENTER")
                && !KeyEvent.getKeyText(evt.getKeyCode()).toUpperCase().matches("BACKSPACE")) {
            JOptionPane.showMessageDialog(dialog, "Format masukan salah\nnomor kamar adalah angka");
        }
    }

    public static void checkOutKamarAction(JTextField nokamarsewa, JTextField namaTamuText,
            JTextField alamatText, JTextField tanggalCheckInText, JTextField tipeKamarText, JTextField jumpenyewaText, JTextField totalHargaText) {
        SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
        TransaksiKamar out = new TransaksiKamar();

        String tgl = format.format(new GregorianCalendar().getTime());

        if (nokamarsewa.getText().equals("")) {

            JOptionPane.showMessageDialog(null, "Nomor kamar belum dimasukkan");
        } else {
            try {
                int nomor = Integer.parseInt(nokamarsewa.getText());
                out.setNomor_kamar(nomor);
                totalHargaText.setText("" + TransaksiKamarController.checkout(nomor, tgl));
                JOptionPane.showMessageDialog(null, "Saudara/i " + namaTamuText.getText() + " Berhasil Checkout");
            } catch (FileNotFoundException ex) {
                JOptionPane.showMessageDialog(null, "File database tidak ditemukan");
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, "Konfigurasi database salah");
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
            }

        }
    }

    public static void printBill(JTextField nokamarsewa, JTextField namaTamuText, JTextField alamatText,
            JTextField tanggalCheckInText, JTextField tipeKamarText, JLabel userIdText,
            JTextField jumpenyewaText, JTextField totalHargaText, JLabel noCheckIn, JButton printButton) {
        try {
            Connection kon = Koneksi.getDBConnection();
            String reportSource = "";
            reportSource = "./Reports/BillHotel.jasper";
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("noTrans", noCheckIn.getText().substring(0, 9));
            params.put("totalBiaya", Long.parseLong(totalHargaText.getText()));
            params.put("idKaryawan", userIdText.getText());
            JasperPrint jasperPrint = JasperFillManager.fillReport(reportSource, params, kon);
            nokamarsewa.setText("");
            namaTamuText.setText("");
            alamatText.setText("");
            tanggalCheckInText.setText("");
            tipeKamarText.setText("");
            jumpenyewaText.setText("");
            totalHargaText.setText("");
            noCheckIn.setText("");
            printButton.setEnabled(false);
            JasperViewer.viewReport(jasperPrint, false);
        } catch (Exception ex) {
        }
    }

    public static void batalNotaKamar(String noTrans) {
        try {
            TransaksiKamar.getKoneksiKontrol().batalTrans(noTrans);
        } catch (Exception ex) {
        }
    }

    public static void exit(JDialog dialog, String noTrans) {
        try {
            TransaksiKamar.getKoneksiKontrol().batalTrans(noTrans);
            dialog.dispose();
        } catch (Exception ex) {
            dialog.dispose();
        }

    }
}
